package com.ruoyi.crm.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.crm.domain.CustomerProduct;
import com.ruoyi.crm.mapper.PurchaseRfqMapper;
import com.ruoyi.crm.domain.PurchaseRfq;
import com.ruoyi.crm.service.IPurchaseRfqService;

/**
 * 采购询价单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-08-31
 */
@Service
public class PurchaseRfqServiceImpl implements IPurchaseRfqService
{
    @Autowired
    private PurchaseRfqMapper purchaseRfqMapper;

    /**
     * 查询采购询价单
     *
     * @param id 采购询价单主键
     * @return 采购询价单
     */
    @Override
    public PurchaseRfq selectPurchaseRfqById(String id)
    {
        return purchaseRfqMapper.selectPurchaseRfqById(id);
    }

    /**
     * 查询采购询价单列表
     *
     * @param purchaseRfq 采购询价单
     * @return 采购询价单
     */
    @Override
    public List<PurchaseRfq> selectPurchaseRfqList(PurchaseRfq purchaseRfq)
    {
        return purchaseRfqMapper.selectPurchaseRfqList(purchaseRfq);
    }

    /**
     * 新增采购询价单
     *
     * @param purchaseRfq 采购询价单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPurchaseRfq(PurchaseRfq purchaseRfq)
    {
        int rows = purchaseRfqMapper.insertPurchaseRfq(purchaseRfq);
        insertCustomerProduct(purchaseRfq);
        return rows;
    }

    /**
     * 修改采购询价单
     *
     * @param purchaseRfq 采购询价单
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePurchaseRfq(PurchaseRfq purchaseRfq)
    {
        purchaseRfq.setUpdateTime(DateUtils.getNowDate());
        purchaseRfqMapper.deleteCustomerProductByCustomerName(purchaseRfq.getId());
        insertCustomerProduct(purchaseRfq);
        return purchaseRfqMapper.updatePurchaseRfq(purchaseRfq);
    }

    /**
     * 批量删除采购询价单
     *
     * @param ids 需要删除的采购询价单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePurchaseRfqByIds(String[] ids)
    {
        return purchaseRfqMapper.deletePurchaseRfqByIds(ids);
    }

    /**
     * 删除采购询价单信息
     *
     * @param id 采购询价单主键
     * @return 结果
     */
    @Override
    public int deletePurchaseRfqById(String id)
    {
        purchaseRfqMapper.deleteCustomerProductByCustomerName(id);
        return purchaseRfqMapper.deletePurchaseRfqById(id);
    }

    /**
     * 新增顾客产品信息信息
     *
     * @param purchaseRfq 采购询价单对象
     */
    public void insertCustomerProduct(PurchaseRfq purchaseRfq)
    {
        List<CustomerProduct> customerProductList = purchaseRfq.getCustomerProductList();
        String id = purchaseRfq.getId();
        if (StringUtils.isNotNull(customerProductList))
        {
            List<CustomerProduct> list = new ArrayList<CustomerProduct>();
            for (CustomerProduct customerProduct : customerProductList)
            {
                customerProduct.setCustomerName(id);
                list.add(customerProduct);
            }
            if (list.size() > 0)
            {
                purchaseRfqMapper.batchCustomerProduct(list);
            }
        }
    }
}